<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\db\Query;
use think\Session;

class Business extends Controller
{
    public function index($attentId)
    {

        $id = Session::get('id');
        $user = Session::get('username');
       // halt($user);
        //$attentId = $_GET['attentId'];
        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id=' . $attentId)
            ->where('b.role=2')
            ->find();
            // halt($info);
        $articles = Db::table(['wb_buser' => 'b', 'wb_articles' => 'a'])
            ->field(['b.id', 'b.username', 'a.title', 'a.description', 'a.thumbnail', 'a.content', 'a.uid', 'a.status', 'b.role'])
            ->where('b.id = a.uid  and b.id=' . $attentId)
            ->where('b.role=2')
            ->select();

//            halt($articles);
        $bad = Db::table(['wb_buser' => 'b', 'wb_bad' => 'd'])
            ->field(['b.id', 'b.username', 'd.title', 'd.path', 'd.uid', 'd.link'])
            ->where('b.id=d.uid and b.role=2 and b.id=' . $attentId)
            ->select();
//            halt($bad);

            $attent = Db::table('wb_attent a')
                ->join('wb_buser b', 'a.attent_id=b.id')
                ->join('wb_buserinfo i', 'i.uid=b.id')
                ->field(['i.photo', 'b.username','a.attent_id','b.role'])
                ->where('a.user_id=' . $attentId)
                ->select();

        $fans = Db::table('wb_fans f')
            ->join('wb_buser b', 'f.fans_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username', 'f.fans_id', 'b.role'])
            ->where('f.user_id=' . $attentId)
            ->select();
//            halt($fans);

            $message = Db::table('wb_message m')
                ->join('wb_buser b', 'm.uid=b.id', 'left')
                ->join('wb_mpicture p', 'p.mid=m.id ', 'left')
                ->join('wb_buserinfo i','b.id=i.uid','left')
                ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username', 'm.id', 'group_concat(p.path) path','i.photo','b.id uid'])
                ->where('b.id=' . $attentId)
                ->where('m.uid='. $attentId)

                ->order('m.id desc')
                ->group('m.id')
                ->select();

//            halt($message);
        foreach ($message as $k => $v) {

            if (!empty($v['src_id'])) {
//                halt($v['src_id']);
                $res1 = Db::field('m.id id,u.username username,photo,content,time,m.id uid')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u', 'wb_buserinfo' => 'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = ' . $v['src_id'])
                    ->find();
//                halt($res1);
                $message[$k]['old'] = $res1;
                $path = Db::field('path')
                    ->table(['wb_message' => 'm', 'wb_mpicture' => 'mp'])
                    ->where('m.id = ' . $v['src_id'] . ' and mp.mid = m.id')
                    ->find();
                if (!empty($path)) {
                    $message[$k]['old']['path'] = $path;
                }
            }

        }
        if (!empty($id)) {
            $attentStatus = $this->isAttent($id, $attentId);
        } else {
            $attentStatus = 1;
        }
        
        if (empty($id)) {
            
           return view('index@business/index', [
                'attentStatus' => $attentStatus,
                'info' => $info,
                'articles' => $articles,
                'bad' => $bad,
                'attent' => $attent,
                'fans' => $fans,
                'message' => $message,
                'attentId' => $attentId,
                'id' => $id,
            ]);
        }
        $arr = Db::field('role')
                ->table(['wb_buser'])
                ->where('id = ' . $id)
                ->find();

        $role = implode("", $arr);  
        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i', 'wb_buser b'])
            ->where('i.uid = ' . $id)
            ->where('b.id=i.uid')
            ->find();

        if (!empty($p) ){
            $photo = implode("", $p);
        }
        $id_p = Session::get('id');
            $pto = Db::table('wb_buserinfo')->field('photo')->where('uid',$id_p)->find();
        return view('index@business/index', [
            'attentStatus' => $attentStatus,
            'info' => $info,
            'articles' => $articles,
            'bad' => $bad,
            'attent' => $attent,
            'fans' => $fans,
            'message' => $message,
            'attentId' => $attentId,
            'id' => $id,
            'photo' => $photo,
            'user' => $user,
            'pto'=>$pto
        ]);
    }


    public function server($attentId)
    {
        $user = Session::get('username');
        $id = Session::get('id');
        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id=' . $attentId)
            ->where('b.role=2')
            ->find();
        $articles = Db::table(['wb_buser' => 'b', 'wb_articles' => 'a'])
            ->field(['b.id', 'b.username', 'a.title', 'a.description', 'a.thumbnail', 'a.content', 'a.uid', 'a.status', 'b.role'])
            ->where('b.id = a.uid  and b.id=' . $attentId)
            ->where('b.role=2')
            ->select();

        if (empty($id)) {
           return view('index@business/server', [
                
                'articles' => $articles,
                'info' => $info,
                'attentId' => $attentId,
                'id' => $id,
                'user'=>$user
            ]);
        }
        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i', 'wb_buser b'])
            ->where('i.uid = ' . $id)
            ->where('b.id=i.uid')
            ->find();

        if (!empty($p)) {
            $photo = implode("", $p);
        }
        
        return view('index@business/server',[
            'articles' => $articles,
            'info' => $info,
            'attentId' => $attentId,
            'photo' => $photo,
            'id' => $id,
            'user'=>$user
        ]);

    }

    public function photo($attentId)
    {

        $id = Session::get('id');
        $user = Session::get('username');
        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id= ' . $attentId)

            ->where('b.role=2')
            ->find();

        $photofolder = Db::table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
            ->field(['b.id', 'b.username', 'p.name'])
            ->where('b.id=p.uid and b.id=' . $attentId)
            ->select();

        if (empty($id)) {
          return view('/business/photo', [
                'info' => $info,
                'photofolder' => $photofolder,
                'attentId' => $attentId,
                'id' => $id,
                'user'=>$user
            ]);
        }

        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i', 'wb_buser b'])
            ->where('i.uid = ' . $id)
            ->where('b.id=i.uid')
            ->find();
        $photo = implode("", $p);
        return view('/business/photo', [
            'info' => $info,
            'photofolder' => $photofolder,
            'attentId' => $attentId,
            'id' => $id,
            'photo'=>$photo,
            'user'=>$user
        ]);


    }

    // 查询是否关注
    public function isAttent($id, $attentId)
    {
        $attent = Db::table(['wb_attent' => 'a'])
            ->field(['a.user_id', 'a.attent_id'])
            ->where('a.user_id=' . $id)
            ->where(' a.attent_id=' . $attentId)
            ->find();
//            halt($attent);
        $fans = Db::table(['wb_fans' => 'f'])
            ->field(['f.user_id', 'f.fans_id'])
            ->where('f.user_id=' . $attentId)
            ->where('fans_id=' . $id)
            ->find();
//            halt($fans);
        $both = Db::table(['wb_attent' => 'a', 'wb_fans' => 'f'])
            ->where('a.user_id=f.user_id and a.attent_id=f.fans_id')
            ->find();
//            halt($both);
        if (null != $attent) {
            if (null != $fans && null != $both) {
                $attentStatus = 3;
            } else {
                $attentStatus = 2;
            }
        } else {
            $attentStatus = 1;
        }
        return $attentStatus;
    }


//添加关注
    public function checkData($userId, $attentId)
    {
        $res = Db::table(['wb_attent' => 'a'])
            ->field(['a.attent_id'])
            ->where('a.attent_id=' . $attentId)
            ->where('a.user_id=' . $userId)
            ->select();

        $attentRes = '';
        if (null == $res) {
            $data = ['user_id' => $userId, 'attent_id' => $attentId];
            $attentRes = Db::table(['wb_attent'])
                ->insert($data);

            $data1 = ['user_id' => $attentId, 'fans_id' => $userId];
            $attentRes = Db::table(['wb_fans'])
                ->insert($data1);
        }


    }

// 添加粉丝
    public function checkData1($userId, $attentId)
    {
        $res1 = Db::table(['wb_fans' => 'f'])
            ->field(['f.fans_id'])
            ->where('f.fans_id=' . $userId)
            ->where('f.user_id=' . $attentId)
            ->select();

        $fansRes = '';
        if (null == $res1) {
            $data1 = ['user_id' => $attentId, 'fans_id' => $userId];
            $attentRes = Db::table(['wb_fans'])
                ->insert($data1);
        }

    }

    // 关注
    public function attent()
    {
        $attentId = $_GET['attentId'];
        $user_id = $_GET['user_id'];
        if (!empty($user_id)) {
            $this->checkData($user_id, $attentId);
            $this->checkData1($user_id, $attentId);
        }
        $attentStatus = $this->isAttent($user_id, $attentId);
        return json([
            'id' => $user_id,
            'attentId' => $attentId,
            'attentStatus' => $attentStatus
        ]);


    }

    //取消关注
    public function delAttent()
    {
        $attentId = $_GET['attentId'];
        $user_id = $_GET['user_id'];
        $res = Db::table(['wb_attent' => 'a'])
            ->field(['a.attent_id'])
            ->where('a.attent_id=' . $attentId)
            ->where('a.user_id=' . $user_id)
            ->select();
        if (null != $res) {
            $attentRes = Db::table(['wb_attent'])
                ->where('user_id=' . $user_id)
                ->where('attent_id=' . $attentId)
                ->delete();
            $fansRes = Db::table(['wb_fans'])
                ->where('user_id=' . $attentId)
                ->where('fans_id=' . $user_id)
                ->delete();
        }
        $attentStatus = $this->isAttent($user_id, $attentId);
        return json([
            'id' => $user_id,
            'attentId' => $attentId,
            'attentStatus' => $attentStatus
        ]);
    }

    public function other($attent_id)
    {
//halt($_GET);
        $id = Session::get('id');

        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id=' . $attent_id)
            ->where('b.role=1')
            ->find();
//        halt($id);
        //$attentId = $_GET['attentId'];
        $list = Db::field(['b.username', 'b.id', 'p.name', 'p.uid'])
            ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
            ->where('b.id= ' . $attent_id . ' and b.id = p.uid')
            ->select();

        $message = Db::table('wb_message m')
            ->join('wb_buser b', 'm.uid=b.id', 'left')
            ->join('wb_mpicture p', 'p.mid=m.id ', 'left')
            ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username', 'm.id', 'group_concat(p.path) path'])
            ->where('b.id=' . $attent_id)
            ->where('m.uid=' . $attent_id)
            ->group('m.id')
            ->select();

        $at = Db::field('id,title,thumbnail')
            ->table('wb_articles')
            ->select();

        $attent = Db::table('wb_attent a')
            ->join('wb_buser b', 'a.attent_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username', 'b.role', 'a.attent_id'])
            ->where('a.user_id=' . $attent_id)
            ->select();
        $fans = Db::table('wb_fans f')
            ->join('wb_buser b', 'f.fans_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username', 'b.role', 'f.fans_id'])
            ->where('f.user_id=' . $attent_id)
            ->select();
        if (!empty($id)) {
            $attentStatus = $this->isAttent($id, $attent_id);
        } else {
            $attentStatus = 1;
        }

        return view('index@cuser/other', [
            'list' => $list,
//            'username' =>$username,
//            'photo'=>$photo,
            'message' => $message,
            'at' => $at,
            'attent' => $attent,
            'fans' => $fans,
            'attent_id' => $attent_id,
            'attentStatus' => $attentStatus,
            'info' => $info
        ]);
    }


}
